package demo;

import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 * @ClassName JTreeTest
 * @Description TODO
 * @Author Li
 * @Date 2022/8/20 14:12
 * @ModifyDate 2022/8/20 14:12
 * @Version 1.0
 */
public class JSplitPaneDemo extends JFrame {
    private static ClassLoader loader= JSplitPaneDemo.class.getClassLoader();
    private static Icon closedIcon = new ImageIcon("/images/closed.png");
    private static Icon openIcon = new ImageIcon("/images/open.png");
    private static Icon leafIcon = new ImageIcon("/images/map.png");

    public JSplitPaneDemo() {
        treeInit();
    }

    private void treeInit() {
        // 设置窗体可见
        this.setVisible(true);
        this.setTitle("demo.JTreeDemo");

        // 设置居中显示
        /*Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕尺寸
        int offsetX = (screenSize.width - 800) / 2;
        int offsetY = (screenSize.height - 600) / 2;
        this.setLocation(offsetX, offsetY);*/


        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createTree(), createTextArea());
        splitPane.setDividerSize(3);
        splitPane.setDividerLocation(150); // 设置分隔线的的位置

        this.getContentPane().add(splitPane);

        // 设置窗体大小
        this.setSize(800, 600);
        // 设置屏幕居中，要放在设置大小的后面
        this.setLocationRelativeTo(null);

        // 设置窗体完全充满整个屏幕的可见大小
        //this.setExtendedState(JFrame.MAXIMIZED_BOTH);

        // 点击退出程序
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 大小设置为可改变
        this.setResizable(true);
    }

    private JScrollPane createTree() {
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBorder(null);
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("中国");
        String Areas[][] = new String[3][];
        Areas[0] = new String[]{"北京", "上海", "广州", "深圳"};
        Areas[1] = new String[]{"江西1"};
        Areas[2] = new String[]{"河北"};
        String gradeNames[] = {"一线城市","落后地区","疫情地域"};
        DefaultMutableTreeNode node = null;
        DefaultMutableTreeNode childNode = null;
        int length = 0;
        for (int i = 0; i < 3; i++) {
            length = Areas[i].length;
            node = new DefaultMutableTreeNode(gradeNames[i]);
            for (int j = 0; j < length; j++) {
                childNode = new DefaultMutableTreeNode(Areas[i][j]);
                node.add(childNode);
            }
            root.add(node);
        }
        JTree tree = new JTree(root);
        JSplitPaneDemo.changeTreeUI(tree);
        scrollPane.add(tree);
        scrollPane.setViewportView(tree);
        tree.setRootVisible(false);
        scrollPane.setVisible(true);
        return scrollPane;
    }

    private JScrollPane createTextArea(){
        JTextArea textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setBorder(null);
        return scrollPane;
    }

    public static void changeTreeUI(JTree tree) {
        //tree.setRootVisible(false);//是否显示根节点
        tree.setRowHeight(30);
        tree.setShowsRootHandles(true);//是否显示句柄
        // 设置 tree 的节点图标
        DefaultTreeCellRenderer render = (DefaultTreeCellRenderer)(tree.getCellRenderer());
        // 叶节点图标
        render.setLeafIcon(leafIcon);
    }

    public static void main(String[] args) {
        try{
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
            com.lijinjiang.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();

        }catch (Exception e){
        }
        new JSplitPaneDemo();
    }
}
